package com.chachazhan.logback.kestrel;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import com.chachazhan.logback.kestrel.formatter.Formatter;
import com.chachazhan.logback.kestrel.formatter.MessageFormatter;

/**
 * logback appender for kestrel
 *
 * @author mingqi.zhou
 */
public class kestrelAppender extends AppenderBase<ILoggingEvent> {

    private String kestrelHost;
    private Formatter formatter;
    private String queueName;

    private KestrelClient client;


    public String getKestrelHost() {
        return kestrelHost;
    }

    public void setKestrelHost(String kestrelHost) {
        this.kestrelHost = kestrelHost;
    }

    public Formatter getFormatter() {
        return formatter;
    }

    public void setFormatter(Formatter formatter) {
        this.formatter = formatter;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    @Override
    public void start() {
        if (this.formatter == null) {
            this.formatter = new MessageFormatter();
        }
        super.start();
        client = new KestrelClient(kestrelHost);
    }

    @Override
    public void stop() {
        super.stop();
        client.shutdown();
    }

    @Override
    protected void append(ILoggingEvent event) {
        String message = this.formatter.format(event);
        client.put(queueName, message);
    }
}
